package com.nine.controller;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nine.entity.Permission;
import com.nine.entity.Roles;
import com.nine.entity.UsersWithBLOBs;
import com.nine.service.RolesService;

@Controller
public class RolesController {
	
	@Autowired
	private RolesService rolesService;
	
	//查询所有角色信息
	@RequestMapping("getAllRoles")
	@ResponseBody
	public Map<String, Object> getAllRoles(Integer page,Integer rows) {
		
		//1.启用分页
		 Page page1=PageHelper.startPage(page, rows); //传页码，页大小
		 List<Roles> list = rolesService.getAllRoles();
		 PageInfo<Roles> pageInfo=new PageInfo<Roles>(list);
		//创建键值对集合
		 Map<String, Object> map=new HashMap<String,Object>();
		 map.put("total", pageInfo.getTotal());  
		 map.put("rows", pageInfo.getList());
		return map;
	}
	
	//添加角色信息
	@RequestMapping("insertRolesSelective")
	@ResponseBody
	public Integer insertSelective(Roles roles,HttpSession session) {
		session.setAttribute("username", "admin");
		//从session作用域中获取当前登录的用户名
		roles.setFounder((String)session.getAttribute("username"));
		//获取系统当前时间
		Date date = Calendar.getInstance().getTime();
		roles.setUpdatetime(date);
		int result = rolesService.insertSelective(roles);
		return result;
		
	}
	
	//根据主键有选择性的修改用户信息
	@ResponseBody
	@RequestMapping("updateRolesByPrimaryKeySelective")
	public Integer updateByPrimaryKeySelective(Roles roles) {
		int result = rolesService.updateByPrimaryKeySelective(roles);
		return result;
	}
	
	//多项删除用户
	@ResponseBody
	@RequestMapping("delMoreRoles.do")
	public Integer delMoreRoles(String ids) {
		String[] strings = ids.split(",");
		//将字符串转换为数字数组
		Integer[] arr=new Integer[(strings.length)];
		for (int j = 0; j < strings.length; j++) {
				arr[j]=new Integer(strings[j]);
			
		}
		int result = rolesService.delMoreiRoles(arr);
		return result;
	}
	
	//根据角色id来获取该角色的所有权限
	@RequestMapping("getPermissionsByRoleId")
	public String getPermissionsByRoleId(Integer id,HttpServletRequest request) {
		if(id!=null) {
			List<Permission> permissionsList = rolesService.getPermissionsByRoleId(id);
			List<Permission> unpermissionsList = rolesService.getUnpermissionsByRoleId(id);
			request.setAttribute("unpermissionsList", unpermissionsList);
			request.setAttribute("permissionsList", permissionsList);
			//mv.getModel().put("permissionsList", permissionsList);  
			//mv.addObject("permissionsList", permissionsList);
			return "/m_admin/permission.jsp";
		}
		return null;
	}
	
	
	//根据角色id获取该角色所未拥有的权限
	public String getUnpermissionsByRoleId() {
		
		return null;
	}
	
	
}
